<template>
  <div class="dflex box">
    <div class="box-top dflex">
      <div class="box-top-item" v-for="item of boxTopList" :key="item.title">
        <p class="box-title">{{ item.title }}</p>
        <div class="box-content" :class="item.isEmpty ? 'box--empty' : ''" v-loading="item.isLoading"></div>
      </div>
    </div>
    <div class="box-bottom mgt20 flex">
      <p class="box-title">{{ boxBottom.title }}</p>
      <div class="box-content" :class="boxBottom.isEmpty ? 'box--empty' : ''" v-loading="boxBottom.isLoading"></div>
    </div>
  </div>
</template>

<script setup>
  //  todo - 百分比和趋势图resize不对 初步怀疑是响应式问题（导致图表实例不对 但是饼图又是对的）
  import echartsOption from '@a/javascript/echartsOption';
  import useEcharts from '@c/useEcharts';
  import {ref, reactive, getCurrentInstance, onMounted, onUnmounted} from 'vue';

  const {proxy} = getCurrentInstance();
  const {initChart, drawChart, handleResize, disposeCharts} = useEcharts();
  const initChartInfo = (title, selector, api) => ({title, isEmpty: true, chart: null, isLoading: false, selector, api});
  const boxTopList = ref([
    initChartInfo('已完成订单', '.box-top-item:nth-child(1) .box-content'),
    initChartInfo('已完成订单分类统计', '.box-top-item:nth-child(2) .box-content', proxy.$api.orderManage.getPieData),
    initChartInfo('未完成订单', '.box-top-item:nth-child(3) .box-content'),
    initChartInfo('未完成订单分类统计', '.box-top-item:nth-child(4) .box-content', proxy.$api.orderManage.getPieData)
  ]);
  const boxBottom = reactive(initChartInfo('订单趋势', '.box-bottom .box-content', proxy.$api.orderManage.getLineData));

  const drawChartPie = info => {
    const option = echartsOption.pie;
    drawChart(info, option, data => {
      data = data.sort((a, b) => a.value - b.value);
      option.series[0].data = data;
    });
  };

  // 获得百分比图表
  const drawChartRatio = async infoList => {
    const options = [];
    infoList.map(info => {
      initChart(info);
      info.isEmpty = true;
      info.isLoading = true;
      options.push(proxy.$tool.objectDeepClone(echartsOption.ratio));
    });
    const res = await proxy.$api.orderManage.getRatioData();
    infoList.map(info => (info.isLoading = false));
    if (res.status === 200 && res.data) {
      infoList.map((info, index) => {
        const option = options[index];
        const isUnfinishedStat = info.title.includes('未完成');
        const value = isUnfinishedStat ? res.data.unfinished : res.data.finished;
        option.angleAxis.max = res.data.total;
        option.series[0].data = [value];
        option.title[0].text = value + '单';
        option.title[1].text = ((value / (res.data.total || 1)) * 100).toFixed(2) + '%';
        const color = isUnfinishedStat ? '#EC2F4F' : '#06AE1C';
        option.title[0].textStyle.color = color;
        option.title[1].textStyle.color = color;
        option.series[0].itemStyle.color.colorStops[1].color = color;
        info.isEmpty = !value;
        info.chart.setOption(option, true);
        handleResize(info.chart.resize);
      });
    }
  };

  const drawChartLine = info => {
    const option = echartsOption.line;
    drawChart(info, option, data => {
      data = data.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
      option.xAxis.data = data.map(item => item.date);
      option.series[0].data = data.map(item => item.value);
    });
  };

  onMounted(() => {
    drawChartPie(boxTopList.value[1]);
    drawChartPie(boxTopList.value[3]);
    drawChartRatio([boxTopList.value[0], boxTopList.value[2]]);
    drawChartLine(boxBottom);
  });
  onUnmounted(() => disposeCharts([...boxTopList.value, boxBottom]));
</script>

<style scoped lang="less">
  .box {
    flex-direction: column;
  }
  .box-top {
    justify-content: space-between;
    height: 250px;
    min-height: 250px;
  }
  .box-top-item {
    flex: 1;
    min-width: 250px;
    & + & {
      .mgl20;
    }
  }
  .box-top-item,
  .box-bottom {
    .dflex;
    flex-direction: column;
    .pd15;
    background: @background-color-lighter;
    border-radius: 5px;
  }
  .box-bottom {
    min-height: 300px;
  }
  .box-title {
    .pdb10;
  }
  .box-content {
    .flex;
  }
</style>
